import {asyncRoutes, constantRoutes, resetRouter} from '@/router'
import {getMeun} from "@/api";
import {handlePromise} from "@/utils";

const state = {
  routes: [],
  menuList:[
    {
      path: '/',
      name: 'index',
      hidden:true,
      component: () => import('@/views/index/index'),
      meta: {
        title: '首页',
        isLogin: false, //不需要登录
        header:true,//不需要头部
        footer:true,
        base: '/' + process.env.VUE_APP_WEB_NAME // 设置基准路径为 /trainWeb
      }
    },
    {
      path: '/index',
      name: 'index',
      hidden: true,
      component: () => import('@/views/index/index'),
      meta: {
        title: '首页',
        isLogin: false, //不需要登录
        header:true,//不需要头部
        footer:true,
        base: '/' + process.env.VUE_APP_WEB_NAME // 设置基准路径为 /trainWeb
      }
    },
    {
      path: '/login',
      name: 'login',
      hidden:true,
      component: () => import('@/views/login/index'),
      meta: {
        title: '登录',
        isLogin: false, //不需要登录
        header:false,//不需要头部
        footer:false,
        base: '/' + process.env.VUE_APP_WEB_NAME // 设置基准路径为 /trainWeb
      }
    },
    {
      path: '/training-course-list',
      name: 'training-course-list',
      hidden:true,
      component: () => import('@/views/training-course-list/index'),
      meta: {
        title: '选课中心',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      }
    },
    {
      path: '/course-list',
      name: 'course-list',
      hidden: true,
      component: () => import('@/views/course-list/index'),
      meta: {
        title: '课程中心',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      }
    },
    {
      path: '/studyspace',
      name: 'studyspace',
      hidden:true,
      component: () => import('@/views/studyspace/index'),
      meta: {
        title: '学习中心',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
      children:[
        {
          path: '/studyspace/my-trainclass',
          component: () => import('@/views/studyspace/my-trainclass'),
          name: 'my-trainclass',
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的培训班',
            parent:"studyspace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-required-course',
          component: () => import('@/views/studyspace/my-required-course'),
          name: 'my-required-course',
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的课程',
            parent:"studyspace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-history-course',
          component: () => import('@/views/studyspace/my-history-course'),
          name: 'my-history-course',
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '历史课程',
            parent:"studyspace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-exam',
          name: 'my-exam',
          component: () => import('@/views/studyspace/my-exam'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '在线考试',
            parent:"studyspace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-notice',
          name: 'my-notice',
          component: () => import('@/views/studyspace/my-notice'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的消息',
            parent:"studyspace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-order',
          name: 'my-order',
          component: () => import('@/views/studyspace/my-order'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的订单',
            parent:"studyspace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-cart',
          name: 'my-cart',
          component: () => import('@/views/studyspace/my-cart'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的购物车',
            parent:"studyspace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-certificate',
          name: 'my-certificate',
          component: () => import('@/views/studyspace/my-certificate'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的证书',
            parent:"studyspace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-order-detail',
          name: 'my-order-detail',
          component: () => import('@/views/studyspace/my-order-detail'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的订单详情',
            parent:"studyspace/my-trainclass"
          }
        },
      ],
      default: '/studyspace/my-trainclass',
    },
    {
      path: '/orderSpace',
      name: 'orderSpace',
      hidden:true,
      component: () => import('@/views/studyspace/index'),
      meta: {
        title: '订单中心',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
      children:[
        {
          path: '/orderSpace/my-trainclass',
          component: () => import('@/views/studyspace/my-trainclass'),
          name: 'my-trainclass',
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的培训班',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/orderSpace/my-required-course',
          component: () => import('@/views/studyspace/my-required-course'),
          name: 'my-required-course',
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的课程',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-history-course',
          component: () => import('@/views/studyspace/my-history-course'),
          name: 'my-history-course',
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '历史课程',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/orderSpace/my-exam',
          name: 'my-exam',
          component: () => import('@/views/studyspace/my-exam'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '在线考试',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/orderSpace/my-notice',
          name: 'my-notice',
          component: () => import('@/views/studyspace/my-notice'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的消息',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/orderSpace/my-order',
          name: 'my-order',
          component: () => import('@/views/studyspace/my-order'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的订单',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/studyspace/my-cart',
          name: 'my-cart',
          component: () => import('@/views/studyspace/my-cart'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的购物车',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/orderSpace/my-certificate',
          name: 'my-certificate',
          component: () => import('@/views/studyspace/my-certificate'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的证书',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/orderSpace/my-record',
          name: 'my-record',
          component: () => import('@/views/studyspace/my-record'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '培训档案',
            parent:"orderSpace/my-trainclass"
          }
        },
        {
          path: '/orderSpace/my-order-detail',
          name: 'my-order-detail',
          component: () => import('@/views/studyspace/my-order-detail'),
          meta: {
            isLogin: true, //不需要登录
            header:true,//不需要头部
            footer:true,
            title: '我的订单详情',
            parent:"orderSpace/my-trainclass"
          }
        },
      ],
      default: '/orderSpace/my-order',
    },

    {
      path: '/notice',
      name: 'notice',
      hidden:true,
      component: () => import('@/views/notice/index'),
      meta: {
        title: '通知公告',
        isLogin: false, //不需要登录
        header:true,//不需要头部
        footer:true,
      }
    },
    {
      path: '/officialtrainingAdmin',
      name: 'officialtrainingAdmin',
      hidden:true,
      component: () => import('@/views/notice/index'),
      meta: {
        title: '政策法规',
        isLogin: false, //不需要登录
        header:true,//不需要头部
        footer:true,
      }
    },
    {
      path: '/notice/detail',
      hidden: true,
      name: 'notice-detail',
      component: () => import('@/views/notice/notice-detail'),
      meta: {
        title: '通知公告详情',
        isLogin: false, //不需要登录
        header:true,//不需要头部
        footer:true,
      }
    },
    {
      path: '/help-center',
      name: 'help-center',
      hidden:true,
      component: () => import('@/views/help-center/index'),
      meta: {
        title: '帮助中心',
        isLogin: false, //不需要登录
        header:true,//不需要头部
        footer:true,
      }
    },
    {
      path: '/training-detail',
      name: 'training-detail',
      component: () => import('@/views/training-detail/index'),
      meta: {
        title: '我的培训班详情',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
      hidden: true,
    },
    {
      path: '/choose-course',
      name: 'choose-course',
      component: () => import('@/views/choose-course/index'),
      meta: {
        title: '选课',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
      hidden: true,
    },
    {
      path: '/show-course',
      name: 'show-course',
      component: () => import('@/views/show-course/index'),
      meta: {
        title: '课程展示',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
      hidden: true,
    },
    {
      path: '/certificate',
      name: 'certificate',
      hidden: true,
      component: () => import('@/views/certificate/index'),
      meta: {
        title: '证书查询',
        isLogin: false, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
    },
    {
      path: '/resetPwd',
      component:() => import('@/views/resetPwd/index'),
      hidden: true,
      name: 'resetPwd',
      meta: {
        title: '忘记密码',
        isLogin: false, //不需要登录
        header:false,//不需要头部
        footer:true,
      },
    },
    {
      path: '/register',
      component:() => import('@/views/register/index'),
      hidden: true,
      name: 'register',
      meta: {
        title: '注册',
        isLogin: false, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
    },
    {
      path: '/pay/toPay',
      name: 'pay/toPay',
      component: () => import('@/views/pay/index'),
      meta: {
        title: '订单确认',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
      hidden: true,
    },
    {
      path: '/pay/toPayCourse',
      hidden: true,
      name: 'pay/toPayCourse',
      component: () => import('@/views/pay/pay-course'),
      meta: {
        title: '核对订单信息',
        isLogin: true,
        header:true,
        footer:true,
      },
    },
    {
      path: '/pay/abcPay',
      hidden: true,
      name: 'pay/abcPay',
      component: () => import('@/views/pay/pay-detail'),
      meta: {
        title: '订单详情',
        isLogin: true, //不需要登录
        header:true,//不需要头部
        footer:true,
      },
    },

    {
      path: '/exam',
      component:() => import('@/views/exam/index'),
      hidden: true,
      name: 'exam',
      meta: {
        title: '考试详情',
        isLogin: true, //不需要登录
        header:false,//不需要头部
        footer:false,
      },
    },
    {
      path: '/examAnalysis',
      component:() => import('@/views/exam/examAnalysis'),
      hidden: true,
      name: 'examAnalysis',
      meta: {
        title: '考试解析',
        isLogin: true, //不需要登录
        header:false,//不需要头部
        footer:false,
      },
    },
    {
      path: '/practice',
      component:() => import('@/views/practice/index'),
      hidden: true,
      name: 'practice',
      meta: {
        title: '练习详情',
        isLogin: true, //不需要登录
        header:false,//不需要头部
        footer:false,
      },
    },
    {
      path: '/practiceAnalysis',
      component:() => import('@/views/practice/practiceAnalysis'),
      hidden: true,
      name: 'practiceAnalysis',
      meta: {
        title: '练习解析',
        isLogin: true, //不需要登录
        header:false,//不需要头部
        footer:false,
      },
    },
    {
      path: '/questionnaire',
      component:() => import('@/views/questionnaire/index'),
      hidden: true,
      name: 'questionnaire',
      meta: {
        title: '调查问卷',
        isLogin: true, //不需要登录
        header:false,//不需要头部
        footer:false,
      },
    },
    {
      path: '/questionnaireAnalysis',
      component:() => import('@/views/questionnaire/questionnaireAnalysis'),
      hidden: true,
      name: 'questionnaireAnalysis',
      meta: {
        title: '调查问卷',
        isLogin: true, //不需要登录
        header: false,//不需要头部
        footer: false,
      },
    },
    {
      path: '/studycourseware',
      component:() => import('@/views/courseware-study/index'),
      hidden: true,
      name: 'studycourseware',
      meta: {
        title: '课件学习',
        isLogin: true, //不需要登录
        header:false,//不需要头部
        footer:false,
      },
    },
    {
      path: '/discussion',
      component:() => import('@/views/discussion/index'),
      hidden: true,
      name: 'discussion',
      meta: {
        title: '主题研讨',
        isLogin: true,
        header:false,
        footer:false,
      },
    },
    {
      path: '/course/singleCourseInfo',
      component:() => import('@/views/course-detail/index'),
      hidden: true,
      name: 'course-detail',
      meta: {
        title: '课程详情',
        isLogin: false,
        header:true,
        footer:true,
      },
    },
    {
      path: '/special-info',
      component:() => import('@/views/special-info/index'),
      hidden: true,
      name: 'special-info',
      meta: {
        title: '专题详情',
        isLogin: false,
        header:true,
        footer:true,
      },
    },
    {
      path: '/blind-phone',
      component:() => import('@/views/blind-phone/index'),
      hidden: true,
      name: 'blind-phone',
      meta: {
        title: '绑定手机号',
        isLogin: false,
        header:true,
        footer:true,
      },
    },
    {
      path: '/weak-password',
      component:() => import('@/views/weak-password/index'),
      hidden: true,
      name: 'weak-password',
      meta: {
        title: '弱密码修改',
        isLogin: false,
        header:true,
        footer:true,
      },

    },
    {
      path: '/play',
      component:() => import('@/views/play/index'),
      hidden: true,
      name: 'play',
      meta: {
        title: '直播详情',
        isLogin: false,
        header:false,
        footer:false,
      },
    },
    {
      path: '/playVideo',
      component:() => import('@/views/playVideo/index'),
      hidden: true,
      name: 'playVideo',
      meta: {
        title: '播放详情',
        isLogin: false,
        header:false,
        footer:false,
      },
    },
    {
      path: '/404',
      hidden: true,
      component: () => import('@/views/error-page/404'),
      name: 'Page404',
      meta: {
        title: '404',
        isLogin: false,
        noCache: true
      }
    },
    {
      path: '/401',
      hidden: true,
      component: () => import('@/views/error-page/401'),
      name: 'Page401',
      meta: {
        title: '401',
        noCache: true ,
        isLogin: false,
      }
    },
    {
      path: '*',
      redirect: '/404',
      hidden: true
    },
  ],
  whitelist:localStorage.getItem('whitelist') || [],
  status:0,
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.routes = routes
  },
  SET_STATUS: (state, status) => {
    state.status = status
  },
  SET_WHITELIST(state,mlist){
    try {
      state.whitelist.push(mlist);
      localStorage.setItem('whitelist',JSON.stringify(state.whitelist) )
    }catch (e) {
      localStorage.removeItem('whitelist')
    }

  },
}

const actions = {
  generateRoutes({ commit }, roles) {
    return  new Promise(async (resolve,reject) => {
      var mlist = [];
      var menuMap = new Map();
      var menuList = state.menuList.slice();
      var hrefArr = []
      const [error, result] = await  handlePromise(getMeun());
        mlist = result.data;
        mlist && mlist.forEach((item,index) =>{
          menuMap.set(item.url, item);
          if (item.url.indexOf("http") !== -1 ){
            var hrefObj = {};
            hrefObj.hrefFlag = true;
            hrefObj.path = item.url;
            hrefObj.name = item.columnName;
            hrefObj.meta = {
              title: item.columnName
            }
            // 只在 menuList 中没有该外部链接时才添加
            if (!menuList.some(menu => menu.url === item.url)) {
              hrefArr.push(hrefObj); // 收集外部链接的菜单项
            }
          }

        });
        menuList = menuList.concat(hrefArr);

        menuList.forEach((item,index) =>{
          // console.log(item)
          // 提取 item.path 的路径部分（忽略查询参数）
          const itemPathOnly = new URL(item.path, window.location.href).pathname;
          // 在 menuMap 中查找完整 URL（包括查询参数）
          let obj = null;
          menuMap.forEach((value, key) => {
            // 获取 key 的路径部分，并忽略查询参数
            const keyPathOnly = new URL(key, window.location.href).pathname;
            // 只比较路径部分，忽略查询参数
            if (keyPathOnly === itemPathOnly && !item.hrefFlag) {
              obj = value;
            }
          });
          if (item.hrefFlag) {
            delete item.hidden;
          }
          if (obj  && item.path !== '/') {
            delete item.hidden;
            item.meta.isLogin = obj.isLogin;
            item.meta.title = obj.columnName;
            if (obj.url != item.path) {
              item.path = obj.url
            }
          }else {
            if ((item.path !== 'index' || item.path !== '/') &&  item.hidden){
              item.hidden = true;
            }
          }
          if (item.meta && !item.meta.isLogin && !state.whitelist.includes(item.path)) {
            commit('SET_WHITELIST',item.path);
          }
          commit('SET_ROUTES', menuList);
          commit('SET_STATUS', 1)
        });

        resolve('success')
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
